import 'package:common_utils/common_utils.dart';
import 'package:date_format/date_format.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_picker/Picker.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:flutter_pickers/style/picker_style.dart';
import 'package:fluttertoast/fluttertoast.dart';

class AppDialog {
  /// iOS 风格提示框
  static void showIosAlert(
    BuildContext context, {
    String title: "温馨提示",
    required String content,
    String leftText: "取消",
    String rightText: "确认",
    required final Function onCancel,
    required final Function onConfirm,
  }) {
    showDialog(
        context: context,
        builder: (context) {
          return CupertinoAlertDialog(
            title: Text(
              title,
              style: TextStyle(fontWeight: FontWeight.w200),
            ),
            content: Column(
              children: [
                SizedBox(
                  height: 10,
                ),
                Align(
                  child: Text(content),
                  alignment: Alignment(0, 0),
                )
              ],
            ),
            actions: [
              CupertinoDialogAction(
                child: Text(leftText),
                onPressed: () {
                  Navigator.pop(context);
                  onCancel();
                },
              ),
              CupertinoDialogAction(
                child: Text(rightText),
                onPressed: () {
                  Navigator.pop(context);
                  onConfirm();
                },
              ),
            ],
          );
        });
  }

  /// 安卓 风格提示框
  static void showAnAlert(
    BuildContext context, {
    String title: "温馨提示",
    required String content,
    String leftText: "取消",
    String rightText: "确认",
    required final Function onCancel,
    required final Function onConfirm,
  }) {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text(title),
            content: Text(content),
            actions: <Widget>[
              TextButton(
                child: Text(leftText),
                onPressed: () {
                  Navigator.pop(context);
                  onCancel();
                },
              ),
              TextButton(
                child: Text(rightText),
                onPressed: () {
                  Navigator.pop(context);
                  onConfirm();
                },
              ),
            ],
          );
        });
  }

  /// 提示文字
  static void showText(
    BuildContext context, {
    required String title,
    double textFont: 14.0,
  }) {
    Fluttertoast.showToast(
        msg: title,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.black87,
        textColor: Colors.white,
        fontSize: textFont);
  }

  /// 一个按钮
  static void showOneBtnAlert(
    BuildContext context, {
    String title: "温馨提示",
    required String content,
    String rightText: "确认",
    required final Function onConfirm,
  }) {
    showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: Column(
              children: [
                Padding(
                  padding: EdgeInsets.only(bottom: 10),
                  child: Text(title,
                      style: TextStyle(
                          color: Colors.black,
                          fontSize: 16,
                          fontWeight: FontWeight.w100)),
                ),
                Text(content,
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 14,
                        fontWeight: FontWeight.w100)),
              ],
            ),
            titlePadding: EdgeInsets.fromLTRB(10, 20, 10, 20),
            contentPadding: EdgeInsets.zero,
            children: [
              Divider(
                height: 1,
              ),
              TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                    onConfirm();
                  },
                  child: Container(
                    width: 100,
                    height: 40,
                    alignment: Alignment.center,
                    child: Text(
                      rightText,
                      style: TextStyle(color: Colors.blue, fontSize: 14),
                    ),
                  )),
            ],
          );
        });
  }

  /// 自定义弹窗
  static void showCusoumAlert(
    BuildContext context, {
    String title: "温馨提示",
    required String content,
    String leftText: "取消",
    String rightText: "确认",
    required final Function onCancel,
    required final Function onConfirm,
  }) {
    showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            titlePadding: EdgeInsets.only(top: 15, bottom: 5),
            title: Center(
              child: Text(title),
            ),
            backgroundColor: Colors.white,
            shape:
                RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
            children: [
              Divider(
                height: 1,
              ),
              Container(
                alignment: Alignment.center,
                height: 80,
                child: Text(content,
                    style: TextStyle(fontSize: 17, color: Colors.black)),
              ),
              Container(
                  child: Row(
                children: [
                  Expanded(
                    flex: 2,
                    child: TextButton(
                      onPressed: () {
                        Navigator.pop(context);
                      },
                      child: Container(
                        alignment: Alignment.center,
                        height: 50,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(10),
                          color: Colors.white,
                          border:
                              Border.all(color: Color(0xffFF4B38), width: 1),
                        ),
                        child: Text('否',
                            style: TextStyle(
                                color: Color(0xffFF4B38), fontSize: 17)),
                      ),
                    ),
                  ),
                  Expanded(
                      flex: 3,
                      child: TextButton(
                          onPressed: () {
                            onCancel();
                            Navigator.pop(context);
                          },
                          child: Container(
                            alignment: Alignment.center,
                            height: 50,
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(10),
                              color: Color(0xffFF4B38),
                              // border: Border.all(color: Color(0xffFF4B38),width: 1),
                            ),
                            child: Text('是',
                                style: TextStyle(
                                    color: Colors.white, fontSize: 17)),
                          )))
                ],
              ))
            ],
          );
        });
  }

  /// 底部弹出
  static void showBottomSheet(
    BuildContext context, {
    String title: "温馨提示",
    required String content,
    String leftText: "取消",
    String rightText: "确认",
    required final Function onCancel,
    required final Function onConfirm,
  }) {
    showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 200,
            child: Column(
              children: <Widget>[
                ListTile(
                  leading: Icon(Icons.settings),
                  title: Text("设置"),
                  onTap: () => Navigator.pop(context),
                ),
                ListTile(
                  leading: Icon(Icons.home),
                  title: Text("主页"),
                  onTap: () => Navigator.pop(context),
                ),
                ListTile(
                  leading: Icon(Icons.message),
                  title: Text("信息"),
                  onTap: () => Navigator.pop(context),
                ),
              ],
            ),
          );
        });
  }

  /// Picker view

  /// 单列数据选择
  static void showSinglePicker(
    BuildContext context, {
    String title: "温馨提示",
    String leftText: "取消",
    String rightText: "确认",
    required final List<dynamic> data,
    required final Function onCancel,
    required final Function onConfirm,
    required final GlobalKey<ScaffoldState> scaffoldKey,
  }) {
    Picker picker = Picker(
        adapter: PickerDataAdapter(pickerData: data),
        changeToFirst: true,
        hideHeader: false,
        title: Text(
          title,
          style: TextStyle(fontSize: 14),
        ),
        textAlign: TextAlign.left,
        cancelText: leftText,
        confirmText: rightText,
        cancelTextStyle: TextStyle(fontSize: 14, color: Colors.grey),
        confirmTextStyle: TextStyle(fontSize: 14, color: Colors.black),
        textStyle: TextStyle(color: Colors.grey, fontSize: 16),
        selectedTextStyle: TextStyle(
            color: Colors.black, fontSize: 18, fontWeight: FontWeight.bold),
        columnPadding: const EdgeInsets.all(8.0),
        onCancel: () {
          onCancel();
        },
        onConfirm: (Picker picker, List value) {
          onConfirm(picker.getSelectedValues());
          // print(value.toString());
          // print(picker.getSelectedValues());
        });

    picker.show(scaffoldKey.currentState!);
  }

  /// 黑色背景
  /// 单列数据选择
  static void showSinglePickerModal(
    BuildContext context, {
    String title: "",
    String leftText: "取消",
    String rightText: "确认",
    required final List<dynamic> data,
    required final Function onCancel,
    required final Function onConfirm,
    required final GlobalKey<ScaffoldState> scaffoldKey,
  }) async {
    final result = await Picker(
        backgroundColor: Colors.transparent,
        headerDecoration: BoxDecoration(
            border:
                Border(bottom: BorderSide(color: Colors.black12, width: 0.5))),
        adapter: PickerDataAdapter<String>(pickerData: data),
        changeToFirst: true,
        hideHeader: false,
        title: Text(
          title,
          style: TextStyle(fontSize: 14),
        ),
        textAlign: TextAlign.left,
        cancelText: leftText,
        confirmText: rightText,
        cancelTextStyle: TextStyle(
            fontSize: 18, color: Colors.black, fontWeight: FontWeight.bold),
        confirmTextStyle: TextStyle(fontSize: 16, color: Colors.black87),
        textStyle: TextStyle(color: Colors.grey, fontSize: 16),
        selectedTextStyle: TextStyle(
            color: Colors.black, fontSize: 18, fontWeight: FontWeight.bold),
        columnPadding: const EdgeInsets.all(8.0),
        containerColor: Colors.white,
        // builderHeader: (BuildContext context){
        //   return Container(
        //     height: 50.0,
        //     decoration: BoxDecoration(
        //         borderRadius: BorderRadius.only(
        //             topRight: Radius.circular(14.0),
        //             topLeft: Radius.circular(14.0)), //
        //         // color: Color.fromRGBO(250, 249, 250, 1),
        //       color: Colors.pink,
        //     ),
        //   );
        // },

        headerColor: Colors.blue,
        onConfirm: (picker, value) {
          print(value.toString());
          print(picker.adapter.text);
          onConfirm(picker.getSelectedValues());
        }).showModal(context); //_sca
    print("result: $result"); //
  }

  static void showPickerDateTime(
    BuildContext context, {
    String title: "",
    String leftText: "选择生日",
    String rightText: "确认",
    required final Function onCancel,
    required final Function onConfirm,
    required final GlobalKey<ScaffoldState> scaffoldKey,
  }) {
    Picker(
            adapter: DateTimePickerAdapter(
              type: PickerDateTimeType.kYMD,
              isNumberMonth: true,
              //strAMPM: const["上午", "下午"],
              yearSuffix: "年",
              monthSuffix: "月",
              daySuffix: "日",
              hourSuffix: "時",
              minuteSuffix: "分",
              secondSuffix: "秒",
              minValue: DateTime(1970, 1, 1),
              minuteInterval: 30,
              //minHour: 1,
              //maxHour: 23,
              // twoDigitYear: true,
            ),
            title: Text(""),
            cancelText: leftText,
            confirmText: rightText,
            backgroundColor: Colors.white,
            cancelTextStyle: TextStyle(
                fontSize: 18, color: Colors.black, fontWeight: FontWeight.bold),
            confirmTextStyle: TextStyle(fontSize: 16, color: Colors.black87),
            textAlign: TextAlign.right,
            selectedTextStyle: TextStyle(color: Colors.black),
            delimiter: [
              PickerDelimiter(
                  column: 5,
                  child: Container(
                    width: 16.0,
                    alignment: Alignment.center,
                    child:
                        Text('', style: TextStyle(fontWeight: FontWeight.bold)),
                    color: Colors.white,
                  ))
            ],
            // footer: Container(
            //   height: 50.0,
            //   alignment: Alignment.center,
            //   child: Text('Footer'),
            // ),
            // selectionOverlay: Container(
            //   height: 50.0,
            //   alignment: Alignment.center,
            //   color: Colors.red,
            //   child: Text('Footer'),
            // ),
            // headerColor: Colors.yellow,
            headerDecoration: BoxDecoration(
              borderRadius: BorderRadius.only(
                  topRight: Radius.circular(14.0),
                  topLeft: Radius.circular(14.0)), //
              color: AppColors.line,
            ),
            onConfirm: (Picker picker, List value) {
              print(formatDate(DateTime.parse(picker.adapter.text),
                  [yyyy, '年', mm, '月', dd, '日']));
              print(picker.adapter.text);
              onConfirm(formatDate(DateTime.parse(picker.adapter.text),
                  [yyyy, '年', mm, '月', dd, '日']));
            },
            onSelect: (Picker picker, int index, List<int> selected) {
              // setState(() {
              //   stateText = picker.adapter.toString();
              // });
            })
        .show(scaffoldKey.currentState!);
  }

  /// 时间背景带圆角
  static void showPickerDateTimeRoundBg(
    BuildContext context, {
    String title: "",
    String leftText: "选择生日",
    String rightText: "确认",
    required final Function onCancel,
    required final Function onConfirm,
    required final GlobalKey<ScaffoldState> scaffoldKey,
  }) {
    var picker = Picker(
        backgroundColor: Colors.transparent,
        headerDecoration: BoxDecoration(
            border:
                Border(bottom: BorderSide(color: Colors.black12, width: 0.5))),
        adapter: DateTimePickerAdapter(
            type: PickerDateTimeType.kYMD,
            isNumberMonth: true,
            yearSuffix: "年",
            monthSuffix: "月",
            daySuffix: "日"),
        delimiter: [
          PickerDelimiter(
              column: 3,
              child: Container(
                width: 8.0,
                alignment: Alignment.center,
                color: Colors.white,
              )),
          PickerDelimiter(
              column: 5,
              child: Container(
                width: 12.0,
                alignment: Alignment.center,
                child: Text(':', style: TextStyle(fontWeight: FontWeight.bold)),
                color: Colors.white,
              )),
        ],
        title: Text(""),
        cancelText: leftText,
        confirmText: rightText,
        cancelTextStyle: TextStyle(
            fontSize: 18, color: Colors.black, fontWeight: FontWeight.bold),
        confirmTextStyle: TextStyle(fontSize: 16, color: Colors.black87),
        textAlign: TextAlign.right,
        selectedTextStyle: TextStyle(color: Colors.black),
        onConfirm: (Picker picker, List value) {
          print(picker.adapter.text);
          onConfirm(formatDate(DateTime.parse(picker.adapter.text),
              [yyyy, '年', mm, '月', dd, '日']));
        },
        onSelect: (Picker picker, int index, List<int> selected) {
          // showMsg(picker.adapter.toString());
        });

    showModalBottomSheet(
        context: context,
        backgroundColor: Colors.transparent,
        builder: (context) {
          return Material(
              color: Colors.white,
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(16), topRight: Radius.circular(16)),
              child: Container(
                padding: const EdgeInsets.only(top: 4),
                child: picker.makePicker(null, true),
              ));
        });
  }

  /// 城市选择
  static void showAppAddressPicker(
    BuildContext context, {
    String titleStr: "",
    String leftText: "选择地址",
    String rightText: "确认",
    required final Function onCancel,
    required final Function onConfirm,
  }) {
    double menuHeight = 36.0;
    // Widget _headMenuView = Container(
    //     // color: Colors.grey[700],
    //     color: Colors.white,
    //     height: menuHeight,
    //     child: Row(children: [
    //       Expanded(
    //           child: Center(
    //               child: Text(
    //         '省',
    //         style: TextStyle(color: Colors.grey[700]),
    //       ))),
    //       Expanded(
    //           child: Center(
    //               child: Text(
    //         '市',
    //         style: TextStyle(color: Colors.grey[700]),
    //       ))),
    //       Expanded(
    //           child: Center(
    //               child: Text(
    //         '区',
    //         style: TextStyle(color: Colors.grey[700]),
    //       ))),
    //     ]));

    Widget _cancelButton = Container(
      alignment: Alignment.centerLeft,
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
      margin: const EdgeInsets.only(left: 22),
      decoration: BoxDecoration(
          border: Border.all(color: Colors.transparent, width: 1),
          borderRadius: BorderRadius.circular(4)),
      child: Text(
        leftText,
        style: TextStyle(
            color: Colors.black, fontSize: 18, fontWeight: FontWeight.bold),
      ),
    );

    Widget _commitButton = Container(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
      margin: const EdgeInsets.only(right: 22),
      decoration: BoxDecoration(
          color: Colors.transparent, borderRadius: BorderRadius.circular(4)),
      child: Text(
        rightText,
        style: TextStyle(color: Colors.black87, fontSize: 16),
      ),
    );

    // 头部样式
    Decoration headDecoration = BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
            topLeft: Radius.circular(8), topRight: Radius.circular(16)));

    Widget title = Center(
      child: Text(titleStr, style: TextStyle(color: Colors.blue, fontSize: 14)),
    );

    /// item 覆盖样式
    Widget itemOverlay = Container(
      decoration: BoxDecoration(
        border: Border.symmetric(
            horizontal: BorderSide(color: Colors.grey, width: 0.7)),
      ),
    );

    var pickerStyle = PickerStyle(
      // menu: _headMenuView,
      menuHeight: menuHeight,
      cancelButton: _cancelButton,
      commitButton: _commitButton,
      headDecoration: headDecoration,
      title: title,
      textColor: Colors.black,
      backgroundColor: Colors.white,
      itemOverlay: itemOverlay,
    );

    Pickers.showAddressPicker(
      context,
      initProvince: '北京',
      initCity: '大兴区',
      initTown: '黄村镇',
      pickerStyle: pickerStyle,
      addAllItem: false,
      onConfirm: (p, c, t) {
        onConfirm(p + c + t!);
      },
    );
  }
}
